<?php
echo "基础1(1)<br><br>";
echo "This is truth:\"php is the best language!\"";

echo "<br><br>基础1(2)<br><br>";
echo "\$a是变量";

echo "<br><br>基础2<br><br>";
echo "After a year, the principal and interest are ￥".$ans1=1000*1.0225;//第一年
echo "<br>After two years, the principal and interest are ￥",$ans2=$ans1*1.0279;//第二年

echo "<br><br>基础3<br><br>";
define("pi","3.14");
echo pi,"<br>";
define("root","user");
echo root,"<br>";

echo "<br>函数<br><br>";
function solve1($b,$c)//当a=0时，求解一元一次 方程
{
	$x=-$c/$b;
	return "x=".$x;
}
function solve2($a,$b,$c)//当a!=0时，求解一元二次方程
{
	$temp=$b*$b-4*$a*$c;//判别式
	if($temp<0)//无解
	{
		echo"input again";
		return null;
	}
	elseif($temp==0)//有一解
	{
		$x1=(-$b+sqrt($temp))/(2*$a);
		return "x=".$x1;
	}
	else//有两个不等实数解
	{
		$x1=(-$b+sqrt($temp))/(2*$a);
		$x2=(-$b-sqrt($temp))/(2*$a);
	}
	return "x1=".$x1."<br>x2=".$x2;
}
$a=$_POST["count1"];//二次项系数
$b=$_POST["count2"];//一次项系数
$c=$_POST["count3"];//常数项
if($a==0)//当方程为一元一次方程时
{
	echo solve1($b,$c);
}
if($a!=0)//当方程为一元二次方程时
{
	echo solve2($a,$b,$c);
}

echo "<br><br>数组1<br><br>";
$stu = array("no"=>"23333333","name"=>"ruby","sex"=>"女","tele"=>"888888888");
foreach ($stu as $key => $value)
{
	echo($key."=>".$value."<br>");
}

echo "<br>数组2<br><br>";
$woods=array("first"=>array("id"=>"001","name"=>"book","cost"=>"50"),"second"=>array("id"=>"002","name"=>"bread","cost"=>"5"),"third"=>array("id"=>"003","name"=>"bow1","cost"=>"10"));
foreach($woods as $list => $detail)//购物车中所有商品的列表
{
	if(is_array($detail))
	{
		foreach ($detail as $key => $value)//每个商品的具体信息
		{
			echo ($key."=>".$value."<br>");
		}
		echo "<br>";//每个商品的具体信息显示完后空一行
	}
}

echo "流程控制2<br><br>";
//为了读起来顺口，写得比较繁琐
$chineses=array('','一','二','三','四','五','六','七','八','九');//积小于10时
$chinesem=array('十','十','二','三','四','五','六','七','八','九');//积大于10小于20时
$chinesel=array('','一','二','三','四','五','六','七','八','九');//积大于20时
for($i=1;$i<=9;$i++)
	{
	for($j=1;$j<=$i;$j++)
	{
		$product=$i*$j;//乘积
		if($product<10) 
			echo "{$chineses[$j]}{$chineses[$i]}得{$chineses[$product]}&nbsp;&nbsp;";
		if($product==10) 
			echo "{$chineses[$j]}{$chineses[$i]}一十&nbsp;&nbsp;";
		if($product>10&&$product<=20) 
			echo "{$chinesem[$j]}{$chinesem[$i]}{$chinesem[intval($product/10)]}{$chinesem[$product%10]}&nbsp;&nbsp;";
		if($product>20) 
			echo "{$chinesel[$j]}{$chinesel[$i]}{$chinesel[intval($product/10)]}十{$chinesel[$product%10]}&nbsp;&nbsp;";	
	}
	echo "<br>";
	}

echo "<br><br>流程控制3<br><br>";
$received=$_POST["received"];//应收金额
$collected=$_POST["collected"];//实收金额
echo "应收金额：".$received."元<br>";
echo "实收金额：".$collected."元<br>";
if($received>$collected)//当应收金额大于实收金额时
	{
		echo "WRONG!";
	}//报错
if($received<=$collected)//当应收金额小于等于实收金额时
	{
		$minus=$collected-$received;//实收金额减应收金额
		echo"应找零：<br>";
		echo "100元:".$a=floor($minus/100)."张<br>";
		echo "50元:".$b=floor(($minus-$a*100)/50)."张<br>";
		echo "20元:".$c=floor(($minus-$a*100-$b*50)/20)."张<br>";
		echo "10元:".$d=floor(($minus-$a*100-$b*50-$c*20)/10)."张<br>";
		echo "5元:".$e=floor(($minus-$a*100-$b*50-$c*20-$d*10)/5)."张<br>";
		echo "1元:".$f=floor(($minus-$a*100-$b*50-$c*20-$d*10-$e*5)/1)."张<br>";
	}

echo "<br>流程控制4(1)<br><br>";
$stud = array(0=>88,1=>60,3=>99,5=>66);
foreach ($stud as $number => $score)
{
	echo $number."=>".$score."<br>";
}

echo "<br>流程控制4(2)<br><br>";
$stud = array(0=>88,1=>60,2=>"",3=>99,4=>"",5=>66);
foreach ($stud as $number => $score)
{
	if (empty($score))//分数不存在时
	{
		echo "WRONG";//报错
		break;//跳出循环
	}
	else
	echo $number."=>".$score."<br>";
}
?>